﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CodeCamp.Infrastructure;
using CodeCamp.Models.Sponsors;
using RGoodSW.Logger.Interfaces;

namespace CodeCamp.Controllers
{
  public class SponsorsController : ControllerBase
  {
    public SponsorsController(IDefaultValueProvider defaultValueProvider, ILoggerExtended logger)
      : base(defaultValueProvider, logger)
    {
    }
    
    // GET: /Sponsors/

    public virtual ActionResult List( string eventName )
    {
      using (var repository = new CodeCampDataRepository())
      {
          var ccEvent = EventsHelper.GetEvent(repository, eventName, DefaultEventShortName, this.User);
          SetSharedViewBagProperties(ccEvent);
          int eventId = ccEvent.Id;
          var eventSponsors = repository.GetSponsors(eventId);

          // assemble model
          var sponsors = new List<SponsorsListViewModel.Sponsor>();
          foreach (var sponsor in eventSponsors)
          {
              var size = SponsorsHelper.GetScaledImageSize(sponsor.ImageWidth, sponsor.ImageHeight);

              sponsors.Add(
                new SponsorsListViewModel.Sponsor
                {
                    SponsorId = sponsor.Id,
                    Name = sponsor.Name,
                    Url = sponsor.Url,
                    ImageUrl = sponsor.ImageUrl,
                    ImageWidth = size.Width,
                    ImageHeight = size.Height
                });
          }
          sponsors.PermuteRandomly();

          var communitySponsors = new List<SponsorsListViewModel.CommunitySponsor>();
          foreach (var communitySponsor in repository.GetCommunitySponsors(eventId))
              communitySponsors.Add(
                new SponsorsListViewModel.CommunitySponsor
                {
                    Name = communitySponsor.Name,
                    Url = communitySponsor.Url
                });

          var individualContributors = new List<SponsorsListViewModel.IndividualContributor>();
          foreach (var individualContributor in repository.GetIndividualContributors(eventId))
              individualContributors.Add(
                new SponsorsListViewModel.IndividualContributor
                {
                    Name = string.Format("{0} {1}", individualContributor.FirstName, individualContributor.LastName)
                });

          var model =
            new SponsorsListViewModel
            {
                Sponsors = sponsors,
                CommunitySponsors = communitySponsors,
                IndividualContributors = individualContributors
            };

          //*** Create file XXXXSponsor1.cshtml (where XXXX is event ShortName)
          //*** or XXXXSponsor2.cshtml (where XXXX is event ShortName)
          //*** in the Sponsors\Partial folder
          string partialName = string.Format("Partial/{0}Sponsor1", ccEvent.ShortName);
          ViewEngineResult partial = ViewEngines.Engines.FindView(this.ControllerContext, partialName, null);
          if ((partial != null) && (partial.View != null))
              model.PartialView1 = partialName;

          partialName = string.Format("Partial/{0}Sponsor2", ccEvent.ShortName);
          partial = ViewEngines.Engines.FindView(this.ControllerContext, partialName, null);
          if ((partial != null) && (partial.View != null))
              model.PartialView2 = partialName;

          SetSharedEventProperties(ccEvent, model, repository);

          return View(model);
      }
    }

    // GET: /Sponsors/Details/5

    public ActionResult Details( int id )
    {
      return View();
    }

    // GET: /Sponsors/Create

    public ActionResult Create()
    {
      return View();
    }

    // POST: /Sponsors/Create

    [HttpPost]
    public ActionResult Create( FormCollection collection )
    {
      try
      {
        // TODO: Add insert logic here

        return RedirectToAction( "Index" );
      }
      catch
      {
        return View();
      }
    }

    // GET: /Sponsors/Edit/5

    public ActionResult Edit( int id )
    {
      return View();
    }

    // POST: /Sponsors/Edit/5

    [HttpPost]
    public ActionResult Edit( int id, FormCollection collection )
    {
      try
      {
        // TODO: Add update logic here

        return RedirectToAction( "Index" );
      }
      catch
      {
        return View();
      }
    }
  }
}
